import os
import time
import re
import json

package_path = os.path.dirname(__file__)


def get_reslove_path(path):
  return os.path.join(package_path, path)

def wirte(file_path, str):
  with open(file_path, 'a', encoding='utf-8') as file:
    file.write(str)



def read(file_path, callback):
  with open(file_path, 'r+', encoding='utf-8') as file:

    line = file.readline()

    while line:
      callback(line)
      print('--', line)
      time.sleep(0.5)
      line = file.readline()
  

'''
  提取错误信息内的店铺id
'''

shop_id_list = []


def extract_shop_id(line):
  _d = re.findall(r'jsonp/(\d+)/shopinfo', line)

  if(len(_d) > 0):
    shop_id = _d[0]
    shop_id_list.append(shop_id)
    print('shop_id: ', shop_id)
  


log_path = get_reslove_path('../pp/logs/shop_2021-05-24-copy.log')
wirte_path = get_reslove_path('../pp/logs/error_path_shop.json')


read(log_path, extract_shop_id)
wirte(wirte_path, json.dumps(shop_id_list))

print(shop_id_list)